package com.stylefeng.guns.modular.small.util;

import lombok.extern.slf4j.Slf4j;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 根据反射生成XML
 *
 * @author 喻克
 */
@Slf4j
public class ObjectToXml {
    /**
     * 根据类名反射得到其属性及属性值
     * 属性为XML标签，属性值为XML标签值
     *
     * @param object
     * @return String
     * @throws Exception
     */
    public static String ObjecttoXML(Object object) throws Exception {
        Class<? extends Object> classType = object.getClass();
//属性集合
        Field[] fields = classType.getDeclaredFields();
        String xml = "";
        for (Field field : fields) {
            String fieldName = field.getName();//属性名称
            String stringLetter = fieldName.substring(0, 1).toUpperCase();
// 获得object对象相应的get方法
            String getName = "get" + stringLetter + fieldName.substring(1);
// 获取相应的方法
            Method getMethod = classType.getMethod(getName, new Class[]{});
// 调用源对象的get方法的值
            Object getValue = getMethod.invoke(object, new Object[]{});
            if (null == getValue) {
                getValue = "";
            }
            xml += "<" + fieldName + ">" + getValue + "</" + fieldName + ">";
        }
        xml = "<object>" + xml + "</object>";
        return xml;
    }

    /**
     * 根据类名反射得到其属性及属性值
     * 属性为XML标签，属性值为XML标签值
     *
     * @param objectList
     * @return String
     * @throws Exception
     */
    public static String ObjecttoXML(List<Object> objectList) throws Exception {
        String xml = "";
        xml += "<objects>";
        for (int i = 0; i < objectList.size(); i++) {
            Object object = objectList.get(i);
            Class<? extends Object> classType = object.getClass();
            Field[] fields = classType.getDeclaredFields();
            xml += "<object>";
            for (Field field : fields) {
                String fieldName = field.getName();
                String stringLetter = fieldName.substring(0, 1).toUpperCase();
                String getName = "get" + stringLetter + fieldName.substring(1);
                Method getMethod = classType.getMethod(getName, new Class[]{});
                Object getValue = getMethod.invoke(object, new Object[]{});
                if (null == getValue) {
                    getValue = "";
                }
                xml += "<" + fieldName + ">" + getValue + "</" + fieldName + ">";
            }
            xml += "</object>";
        }
        xml += "</objects>";
        return xml;
    }

    /**
     * XML格式字符串转换为Map
     *
     * @param xml XML字符串
     * @return XML数据转换后的Map
     * @throws Exception
     */
    public static Map<String, String> xmlToMap(String xml) {
        try {
            Map<String, String> data = new HashMap<>();
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            InputStream stream = new ByteArrayInputStream(xml.getBytes("UTF-8"));
            org.w3c.dom.Document doc = documentBuilder.parse(stream);
            doc.getDocumentElement().normalize();
            NodeList nodeList = doc.getDocumentElement().getChildNodes();
            for (int idx = 0; idx < nodeList.getLength(); ++idx) {
                Node node = nodeList.item(idx);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    org.w3c.dom.Element element = (org.w3c.dom.Element) node;
                    data.put(element.getNodeName(), element.getTextContent());
                }
            }
            stream.close();
            return data;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 将Map转换为XML格式的字符串
     *
     * @param data Map类型数据
     * @return XML格式的字符串
     * @throws Exception
     */
    public static String mapToXml(Map<String, String> data) throws Exception {
        try {
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder= documentBuilderFactory.newDocumentBuilder();
            org.w3c.dom.Document document = documentBuilder.newDocument();
            org.w3c.dom.Element root = document.createElement("xml");
            document.appendChild(root);
            for (String key: data.keySet()) {
                String value = data.get(key);
                if (value == null) {
                    value = "";
                }
                value = value.trim();
                org.w3c.dom.Element filed = document.createElement(key);
                filed.appendChild(document.createTextNode(value));
                root.appendChild(filed);
            }
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            transformer.transform(source, result);
            String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
            writer.close();
            return output;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}